from PyQt4 import QtGui, QtCore

class GameControls(QtGui.QWidget):
    def __init__(self, parent = None):
        QtGui.QWidget.__init__(self)
        self.parent = parent
        self.game = self.parent
        self.window = self.parent.parent #gameframe, window
        
        ## create control widgets
        self.time_left = QtGui.QLabel('Seconds Remaining')
        self.time_left.setStyleSheet('QLabel { font: bold 12px;}')
        self.time_left.setAlignment(QtCore.Qt.AlignHCenter)
        self.timer = GameTimer(self)
        self.msg = QtGui.QLabel('Unshuffler v%s\n\n\nAuthors:\nForrest Desjardins\nOlivier Toupin' % self.parent.parent.settings.version)
        self.msg.setStyleSheet('QLabel { font: bold 13px;}')
        self.msg.setAlignment(QtCore.Qt.AlignHCenter)
        self.start_btn = QtGui.QPushButton('Start\nSingle Game')
        self.series_start_btn = QtGui.QPushButton('Start\nSeries Game')
        
        ## set sizing for widgets
        self.timer.setMinimumHeight(50)
        self.timer.setMaximumHeight(50)
        
        groupBox = QtGui.QGroupBox('');
        groupBox.setStyleSheet("QGroupBox { border: 1px solid rgb(185,185,185); \
                                            border-radius: 3px; }");
        
        ## create layout and add widgets
        vbox = QtGui.QVBoxLayout()
        vbox.addWidget(self.timer)
        vbox.addWidget(self.time_left)
        vbox.addStretch()
        vbox.addWidget(self.msg)
        vbox.addStretch()
        vbox.addWidget(self.start_btn)
        vbox.addWidget(self.series_start_btn)
        groupBox.setLayout(vbox)
        
        ## connect signals to slots
        self.connect(self.start_btn, QtCore.SIGNAL('clicked()'), self.parent.doCountDown)
        
        ## adjust layout
        grid = QtGui.QGridLayout()
        grid.addWidget(groupBox, 0, 0)
        grid.setContentsMargins(5,0,0,0)
        self.setLayout(grid)
        

class GameTimer(QtGui.QLCDNumber):
    def __init__(self, parent = None):
        QtGui.QLCDNumber.__init__(self)
        self.parent = parent
        self.game = self.parent.game
        self.window = self.parent.window
        
        self.i = 0
        self.timer = QtCore.QTimer(self)
        self.connect(self.timer, QtCore.SIGNAL('timeout()'), self.showTime)
        
        style = 'QFrame { font: bold 17px; \
                              background-color: rgb(245,245,245); \
                              border: 1px solid rgb(185,185,185); \
                              border-radius: 3px;}'
        self.setStyleSheet(style)
        
        self.time = QtCore.QTime()
        self.time.setHMS(*self.window.settings.game_time)
        self.showTime()
        
    def reset(self):
        self.time.setHMS(*self.window.settings.game_time)
        self.i = 0
        self.showTime()
        
    def showTime(self):
        ## Subtract seconds elapsed from starting time
        new_time = self.time.addSecs(-1.0*self.i).toString()
        self.display(new_time)
        
        ## If player runs out of time, stop the game
        if(new_time == '00:00:00'):
            self.game.stopGame()
            self.reset()
        else:
            self.i+=1
        
    def startTime(self):
        self.timer.start(1000)
        
    def stopTime(self):
        self.timer.stop()